home *** CD-ROM | disk | FTP | other *** search
- /*
- File: LowerToUpperCaseScrap.c
-
- Contains: Source to a FAT scrap translator that converts from lower to upper case
- The source type is always 'TEXT' and the destination type is 'UPPR'.
-
- Copyright: © 1994 by Apple Computer, Inc., all rights reserved.
- */
-
- #include <Types.h>
- #include <MixedMode.h>
- #include <Traps.h>
- #include <Errors.h>
- #include <Memory.h>
- #include <Components.h>
- #include "TranslationExtensions.h"
-
- //___________________________________________________________________________________________________________
- //
- // Structures
- //
-
- // Our magic cookie to tell if our translation list is initialized or not
- #define kModificationDateMagicCookie 0x00000001
-
- // Set up a fixed scrap translation list structure for easy access
- #if defined(powerc) || defined (__powerc)
- #pragma options align=mac68k
- #endif
- struct OurTranslationList
- {
- unsigned long modDate;
- unsigned long groupCount;
- unsigned long sourceCount;
- unsigned long sourceEntrySize;
- ScrapTypeSpec sourceType;
- unsigned long destinationCount;
- unsigned long destinationEntrySize;
- ScrapTypeSpec desinationType;
- };
- #if defined(powerc) || defined(__powerc)
- #pragma options align=reset
- #endif
-
- typedef struct OurTranslationList OurTranslationList;
- typedef OurTranslationList *OurTranslationListPtr, **OurTranslationListHandle;
-
- //___________________________________________________________________________________________________________
- //
- // DoGetScrapTranslationList
- //
- // This routine is called by the Translation Manager when it needs to find out what formats the scrap
- // translator translates between.
- //
- // Enter: self Instance of this translator
- // list Handle to scrap translation list provided by this translator earlier, or empty handle
- // if never set before
- //
- // Exit: list Handle to scrap translation list filled in my this routine
- // returns Any errors that might occur
- //
- pascal ComponentResult DoGetScrapTranslationList(ComponentInstance self,
- ScrapTranslationListHandle list)
- {
- #pragma unused(self)
- OurTranslationListHandle ourList = (OurTranslationListHandle) list; // Easier to reference
- OSErr result = noErr;
-
- // The list never changes here - so see if it's been initialized once by looking
- // for our magic cookie
- if (((*ourList)->modDate) != kModificationDateMagicCookie)
- {
- // Resize the handle to fit our entry
- SetHandleSize((Handle)ourList, sizeof(OurTranslationList));
- if ((result = MemError()) == noErr)
- {
- // Stuff all the values into our fixed list
- (*ourList)->modDate = kModificationDateMagicCookie; // Magic cookie
- (*ourList)->groupCount = 1; // Number groups = 1
- (*ourList)->sourceCount = 1; // Only 1 source type
- (*ourList)->sourceEntrySize = sizeof(ScrapTypeSpec); // Currently always sizeof(ScrapTypeSpec)
- (*ourList)->sourceType.format = 'TEXT'; // Source type is always 'TEXT'
- (*ourList)->sourceType.hint = 0; // Don't care about this
- (*ourList)->destinationCount = 1; // Only 1 destination type
- (*ourList)->destinationEntrySize = sizeof(ScrapTypeSpec); // Currently always sizeof(ScrapTypeSpec)
- (*ourList)->desinationType.format = 'UPPR'; // Destination type is always 'UPPR'
- (*ourList)->desinationType.hint = 0; // Don't care about this
- }
- }
-
- return result;
- }
-
- //___________________________________________________________________________________________________________
- //
- // DoIdentifyScrap
- //
- // This routine is called to identify a scraps contents. In this case, just check to see if the scrap
- // is type TEXT, and if it is, then we know it's TEXT.
- //
- // Enter: self Instance of this translator
- // dataPtr Pointer to data to identify
- // dataLength Size of data referenced by dataPtr
- // dataFormat Provided format for the data referenced by dataPtr
- //
- // Exit: dataFormat Changed format (if applicable)
- // returns noErr is scrap contents are TEXT, otherwise noTypeErr
- //
- pascal ComponentResult DoIdentifyScrap(ComponentInstance self,
- const void* dataPtr,
- Size dataLength,
- ScrapType* dataFormat)
- {
- #pragma unused(self)
- #pragma unused(dataPtr)
- #pragma unused(dataLength)
- OSErr result = noErr;
-
- // We only know TEXT. See if the format is that. If it isn't then we don't translate
- if (*dataFormat != 'TEXT')
- result = noTypeErr;
-
- return result;
- }
-
- //___________________________________________________________________________________________________________
- //
- // DoTranslateScrap
- //
- // This routine is called to perform a scrap translation. It is only called if the source type is TEXT and
- // if DoIdentifyScrap was called as was able to confirm that. Also, it is only called if the destination type
- // is UPPR.
- //
- // This specific translation converts all lower case characters to upper case. Leaves the rest alone.
- //
- // Enter: self Instance of this translator
- // progressRefNum Reference number to progress dialog
- // srcDataPtr Pointer to source data
- // srcDataLength Length of data pointed to by srcDataPtr
- // srcType Type of data pointed to by srcDataPtr (provided via DoIdentifyScrap)
- // srcTypeHint Hint for data pointed to by srcDataPtr (from ScrapTranslationList)
- // dstData Handle to place translated destination data into
- // dstType Type of data to translate srcDataPtr into
- // dstTypeHint Hint for data to translate to (from ScrapTranslationList)
- //
- // Exit: dstData Handle resized and filled in with translated contents
- // returns Any errors that might occur.
- //
- pascal ComponentResult DoTranslateScrap(ComponentInstance self,
- TranslationRefNum progressRefNum,
- const void* srcDataPtr,
- Size srcDataLength,
- ScrapType srcType,
- long srcTypeHint,
- Handle dstData,
- ScrapType dstType,
- long dstTypeHint)
- {
- #pragma unused(self)
- #pragma unused(srcType)
- #pragma unused(srcTypeHint)
- #pragma unused(dstType)
- #pragma unused(dstTypeHint)
-
- OSErr result;
- char* currentCharacter;
- char* lastCharacter;
- char* writeCharacter;
- short count;
- Boolean canceled = false;
-
- // No advertisement. I'm too lazy to create a PICT
- SetTranslationAdvertisement(progressRefNum, nil);
-
- // Resize the destination handle to fit all the translated contents
- SetHandleSize(dstData, srcDataLength);
- if ((result = MemError()) == noErr)
- {
- // Give me a pointer to the first character and the last character in the source buffer
- currentCharacter = (char*) srcDataPtr;
- lastCharacter = currentCharacter + srcDataLength;
-
- // Give me a pointer to the first character in the destination buffer
- // (lock it down first because stupid update may move memory)
- HLock(dstData);
- writeCharacter = (char *)*dstData;
-
- // Keep a count for the progress dialog
- count = 0;
-
- // Loop through each of the characters in the buffer
- while ((currentCharacter <= lastCharacter) && !canceled)
- {
- // Are we in the lower case ASCII range?
- if (('a' <= *currentCharacter) && (*currentCharacter <= 'z'))
- {
- *writeCharacter = (*currentCharacter - ('a' - 'A'));
- }
- else
- *writeCharacter = *currentCharacter;
-
- // Increment the progress bar
- UpdateTranslationProgress(progressRefNum, ++count * (100.00 / srcDataLength), &canceled);
-
- // Next…
- currentCharacter++;
- writeCharacter++;
- }
-
- // Let it wiggle, see it squiggle
- HUnlock(dstData);
- }
-
- return result;
- }
-
- //___________________________________________________________________________________________________________
- //
- // DoGetFileTranslationList
- //
- // Stub required by library link
- //
- pascal ComponentResult DoGetFileTranslationList(ComponentInstance self,
- FileTranslationListHandle translationList)
- {
- #pragma unused(self)
- #pragma unused(translationList)
- return paramErr;
- }
-
- //___________________________________________________________________________________________________________
- //
- // DoIdentifyFile
- //
- // Stub required by library link
- //
- pascal ComponentResult DoIdentifyFile(ComponentInstance self, const FSSpec *theDoc, FileType *docKind)
- {
- #pragma unused(self)
- #pragma unused(theDoc)
- #pragma unused(docKind)
- return paramErr;
- }
-
- //___________________________________________________________________________________________________________
- //
- // DoTranslateFile
- //
- // Stub required by library link
- //
- pascal ComponentResult DoTranslateFile(ComponentInstance self, TranslationRefNum progressRefNum,
- const FSSpec* srcDoc, FileType srcType, long srcTypeHint,
- const FSSpec* dstDoc, FileType dstType, long dstTypeHint)
- {
- #pragma unused(self) /* no translation is actually done */
- #pragma unused(progressRefNum)
- #pragma unused(srcDoc)
- #pragma unused(srcType)
- #pragma unused(srcTypeHint)
- #pragma unused(dstDoc)
- #pragma unused(dstType)
- #pragma unused(dstTypeHint)
- return paramErr;
- }
-
- //___________________________________________________________________________________________________________
- //
- // DoGetTranslatedFilename
- //
- // Stub required by library link
- //
- pascal ComponentResult DoGetTranslatedFilename(ComponentInstance self,
- FileType dstType,
- long dstTypeHint,
- FSSpec* theDocument)
- {
- #pragma unused(self);
- #pragma unused(dstType);
- #pragma unused(dstTypeHint);
- #pragma unused(theDocument);
- return paramErr;
- }
-